package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.exception.DataNotFoundException;
import com.itheima.common.exception.DeletionNotAllowedException;
import com.itheima.mapper.CheckGroupMapper;
import com.itheima.mapper.CheckGroup_ItemMapper;
import com.itheima.mapper.SetmealMapper;
import com.itheima.mapper.Setmeal_CheckgroupMapper;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CheckGroupServiceimpl implements CheckGroupService {
    @Autowired
    CheckGroupMapper checkGroupMapper;
    @Autowired
    CheckGroup_ItemMapper checkGroup_itemMapper;
    @Autowired
    Setmeal_CheckgroupMapper setmeal_checkgroupMapper;

    //删除检查组-----------------------------------
    @Override
    public void deleteById(int id) {
        Integer count = setmeal_checkgroupMapper.countByCheckGroupId(id);
        if (count > 0) {
            throw new DeletionNotAllowedException(MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
        checkGroupMapper.deleteById(id);
        checkGroup_itemMapper.deleteByCheckGroupId(id);
    }

    //通过套餐id来查检查组--------------------------
//有错误，需联动setmeal
    @Override
    public ArrayList<Integer> findCheckGroupIdsById(int id) {
        ArrayList<Integer> ids = setmeal_checkgroupMapper.findCheckGroupIdsById(id);
        return ids;
    }

    //查询所有检查组---------------------------------------
    @Override
    public ArrayList<CheckGroup> findAll() {

        return checkGroupMapper.findAll();
    }

    //查询检查组通过其id-------------------
    @Override
    public CheckGroup findById(int id) {
        CheckGroup checkGroup = new CheckGroup();
        checkGroup.setId(id);
        List<CheckGroup> checkGroups = checkGroupMapper.select(checkGroup);
        // 判断是否查询到数据
        if (checkGroups == null || checkGroups.isEmpty()) {
            throw new DataNotFoundException(MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
        // 返回查询到的数据
        return checkGroups.get(0);


    }

    //-----------------------------添加检查组++++++++++++++++++++++++++++++++++++++
    @Override
    public void addCheckGroup(CheckGroup checkGroup, List<Integer> ids) {
//昨天遇到的是这个问题，外键限制（看不懂了）。。。
        checkGroupMapper.addCheckGroup(checkGroup);// 添加之后要返回主键的，你要配置返回主键

        // 这个要干什么 mybatis 插入数据返回主键
//        List<CheckGroup> temp = checkGroupMapper.getCheckGroup(checkGroup);
//        // 判断关系表中是否有数据 根据检查组id去查询中间表有没有数据，有，存在，要删除 不存在，插入

//        if (ids != null && !ids.isEmpty()) {
      /*  if (ids != null) {
            // 向检查组检查项关系表插入数据
            for (int i = 0; i < ids.size(); i++) {
                checkGroup_itemMapper.addItem(checkGroup.getId(), ids.get(i));
            }
        }*/
        checkGroup_itemMapper.insertBatch(checkGroup.getId(),ids);
    }

    /*废案
    @Override
    public void addGroupById(Integer G_i, Long id) {

        checkGroupMapper.addGroupById(G_i,id);
    }

    @Override
    public CheckGroup getCheckGroup(CheckGroup checkGroup) {
     return  checkGroupMapper.getCheckGroup(checkGroup);
    }

    @Override
    public void addCheckGroup(CheckGroup checkGroup) {
        checkGroupMapper.addCheckGroup(checkGroup);
    }*/
//分页查询---------------------------
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<CheckGroup> page = checkGroupMapper.pageQuery(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(), page.getResult());
    }

    //更新检查组-----------------------------
    @Override
    public void updateCheckGroup(CheckGroup checkGroup, ArrayList<Integer> ids) {
        checkGroupMapper.update(checkGroup);
        //删重
        checkGroup_itemMapper.deleteByCheckGroupId(checkGroup.getId());
        for (int id : ids) {
            checkGroupMapper.addGroupById(checkGroup.getId(), id);
            ;
        }
    }

}
